package ru.develbureau.server;

import java.util.List;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.develbureau.server.model.ClientRequestEntity;
import ru.develbureau.server.model.OfferEntity;
import ru.develbureau.server.persistence.EMF;

public class CleanInsertServlet extends HttpServlet{
	private static final Logger log = Logger.getLogger(CleanInsertServlet.class.getName());
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response){
		EntityManager em = null;
		try{
			em = EMF.get().createEntityManager();
			String cleName = ClientRequestEntity.class.getName();
			Query q = em.createQuery("SELECT cr FROM " + cleName + " cr");
			List<ClientRequestEntity> cleList = q.getResultList();
			if(cleList!=null){
				for(ClientRequestEntity cle : cleList){
					em.remove(cle);
				}
			}
			
			String offerName = OfferEntity.class.getName();
			q = em.createQuery("SELECT oe FROM " + offerName + " oe");
			List<OfferEntity> offerList = q.getResultList();
			if(offerList!=null){
				for(OfferEntity oe : offerList){
					em.remove(oe);
				}
			}
				
		}catch (Exception e) {
			log.throwing("Error while cleaning and inserting data to db", e.getMessage(), e);
		}finally{
			
		}
	}

}
